<?php
/**
 * masters Model
 *
 * PHP Version 5
 *
 * @package    Ecga
 * @subpackage Models
 * @author     blue5tar <blue5tar@163.com>
 * @copyright  2010 Xingchangxinda Inc.
 * @version    SVN:$Id: masters.php 3051 2011-01-11 13:06:18Z sunbo $
 */

namespace Project\Apps\Ecga\Models;


/**
 * masters Model
 *
 * PHP version 5
 *
 * @package   Ecga
 * @author    blue5tar <blue5tar@163.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   Release: 0.1
 */
class Users extends \Project\Models\Abstraction\Ecga\Users
{
    /**
     * 根据 email 取对象
     *
     * @param string $email 邮件地址
     *
     * @return Entity 对象
     * @author Scott Ye
     */
    public function getByEmail($email)
    {
        return current($this->select()->where('userEmail', '=', $email)->fetch(1, 0));
    }

    /**
     * 根据 masterName 取对象
     *
     * @param string $userName 用户名
     *
     * @author v.k
     * @return Entity
     */
    public function getByUserName($userName)
    {
        return current($this->select()->where('userName', '=', $userName)->fetch(1, 0));
    }

    /**
     * 根据 username 或者 email 来获取用户实例
     *
     * @param string $str 用户名或者邮件地址
     *
     * @return Entity
     * @author Weber Liu
     */
    public function getUser($str)
    {
        return strpos($str, '@') === false ? self::getByUserName($str) : self::getByEmail($str);
    }

    /**
     * 加密
     *
     * @param string $password 密码
     * @param string $salt     扰码
     *
     * @return string 加密后的字符串
     * @author Scott Ye
     */
    public function encrypt($password, $salt)
    {
        return md5($password . $salt);
    }

    /**
     * 生成随机6位密码和4位扰码
     *
     * @param string $password 密码
     *
     * @return array 密码信息
     * @author Confu
     */
    public function createPassword($password='')
    {
        $data = array();
        if (empty($password)) {
            $password = substr(uniqid(), -6);
        }
        $data['salt']    = substr(uniqid(), -4);
        $data['unEncryptPassword'] = $password;
        $data['password'] = self::encrypt($password, $data['salt']);
        return $data;
    }
    
	/**
     * 取分页query
     *
     * @return query $query
     * @author sunfengming
     */
    public static function getPageQuery()
    {
    	$userModel = model('Users');
    	
    	return $userModel->query($userModel->filter())->fields('id', 'userName', 'userEmail');
    }
}
